Python Integer (with Examples)
π§ Python Integers β The Whole (Number) Truth with a Twist! π’β
In Python, int
stands for βintegerβ β and it's the rockstar of the number world πΈ.
These are the whole numbers β no fractions, no decimals, just solid digits from the land of +, β, and 0οΈβ£.
Oh, and Python integers? They donβt believe in limits.
Big dreams, big numbers. πͺ
π― Quick Snippet: Let's Play with Some Integersβ
# Declaring integers
x = 5
y = -10
# Performing arithmetic operations
sum_result = x + y
difference_result = x - y
multiplication_result = x * y
quotient_result = x / y
# Exponentiation
power_result = x ** 2 # Raises x to the power of 2
# Modulo operation
remainder_result = x % 3 # Computes the remainder when x is divided by 3
1οΈβ£ What in the World is an Integer in Python?β
Python int
s are:
- Whole numbers β no decimal drama allowed.
- Can be good (positive), bad (negative), or just... neutral (zero π).
- Can be REALLY huge β Python doesn't mind.
- Can use underscores
_
to look cooler and more readable. - Great for doing math tricks: +, β, *, / β bring it on!
x = 10
y = 12345678987654321
z = 12_34_56
print(x) # 10
print(y) # 12345678987654321
print(z) # 123456
2οΈβ£ Not Just Decimal: Meet Octal and Hexadecimal Friends πβ
Python integers know how to party in different number systems too!
- Octal: Uses base 8, starts with
0o
, and dances with digits from 0β7. - Hexadecimal: Uses base 16, starts with
0x
, and parties with 0β9 + AβF (or aβf).
octalInt = 0o22
hexInt = 0xAA
print(octalInt) # 18
print(hexInt) # 170
β οΈ Note: No exponential showoffs here β octal and hex like to keep things grounded.
3οΈβ£ Let's Get Mathy β Arithmetic Operations π²β
3.1 βββοΈβ Basic Operationsβ
Python math is super chill and familiar. But watch out β /
gives a float, even if it looks like an integer division.
Use //
if you want to chop off the decimal part like a boss π.
x = 22
y = 5
print (x + y) # Prints 27
print (x - y) # Prints 17
print (x * y) # Prints 110
print (x / y) # Prints 4.4
print (x // y) # Prints 4
print (x % y) # Prints 2
print ( divmod(x, y) ) # Prints (4, 2)
3.2 π Increment and Decrementβ
No ++
or --
like other languages. Python is more expressive:
x = 10
y = 10
x += 1
print (x) # Prints 11
x += 5
print (x) # Prints 16
y -= 1
print (y) # Prints 9
y -= 5
print (y) # Prints 4
Python: "Why type two plus signs when one +=
will do?"
3.3 π Exponents β Power Upβ
Need to raise something to the power of something else? Say no more:
x = 10
y = 2
print (x ** y) # Prints 100
Boom π₯ Instant math wizardry.
4οΈβ£ What Am I? Check My Type! π΅οΈββοΈβ
Python lets you double-check types β great for when you're debugging and second-guessing your variables like "Are you really an int?"
Using isinstance()
β The Friendly Detectiveβ
x = 42
if isinstance(x, int):
print("x is an integer")
else:
print("x is not an integer")
Using type()
β The Straight Shooterβ
x = 42
if type(x) is int:
print("x is an integer")
else:
print("x is not an integer")
5οΈβ£ Turning Integers into Strings (Without Magic)β
Need to turn a number into a wordy thing?
Use str()
to charm your integer into a string!
integer_value = 42
string_value = str(integer_value)
Want to jazz it up for display?
formatted_string = "The number is {}".format(integer_value)
Now thatβs some well-dressed data. π
6οΈβ£ Turning Strings into Integers (a bit risky β οΈ)β
Use int()
to try turning a string into an actual number.
string_value = '10'
int_value = int( string_value ) # 10
But be careful! Not all strings are numeric:
string_value = "abc"
try:
integer_value = int(string_value)
print(integer_value)
except ValueError:
print(f"Cannot convert '{string_value}' to an integer.")
Avoid the crash. Handle it like a pro. π¨βπ»
π Conclusion: Integer League β Power Level: Unlimited βΎοΈβ
Integers in Python are:
- Fast ποΈ
- Powerful πͺ
- Versatile π©
- Limitless (literally) π
Whether you're counting sheep π or building rocket software π, Python int
s are always ready to crunch numbers for you.
Happy Number-Crunching! π